package edu.chl.grupp11.aegas.controller.imp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.commons.io.FileUtils;

import edu.chl.grupp11.aegas.controller.ErrorControllerFactory;
import edu.chl.grupp11.aegas.controller.IErrorController;
import edu.chl.grupp11.aegas.controller.IPluginController;
import edu.chl.grupp11.aegas.model.AbstractConfig;
import edu.chl.grupp11.aegas.model.ConfigFactory;

public class PluginController implements IPluginController {
	private IErrorController errorController = ErrorControllerFactory
			.getErrorController();
	private AbstractConfig config = ConfigFactory.getSharedConfig();

	@Override
	public File addPlugin() {
		File file = openFileBrowser();

		if (file != null) {
			if (!checkValidPropFile(file)) {
				errorController.setErrorMessage(null, config
						.getResourceBundle().getString("error12"));
				return null;
			}
			return file;
		} else {
			return null;
		}
	}

	private boolean checkValidPropFile(File file) {
		ArrayList<String> propList = new ArrayList<String>();
		ArrayList<String> keyList = new ArrayList<String>();
		keyList.add("file ");
		keyList.add("open_File ");
		keyList.add("open_Folder ");
		keyList.add("open_URL ");
		keyList.add("save ");
		keyList.add("close ");
		keyList.add("tools ");
		keyList.add("language ");
		keyList.add("settings ");
		keyList.add("add_lang_Plugin ");
		keyList.add("help ");
		keyList.add("clear_All ");
		keyList.add("cancel ");
		keyList.add("about ");
		keyList.add("include ");
		keyList.add("yes ");
		keyList.add("no ");
		keyList.add("add_exst ");
		keyList.add("delete ");
		keyList.add("rename ");
		keyList.add("error1 ");
		keyList.add("error2 ");
		keyList.add("error3 ");
		keyList.add("error4 ");
		keyList.add("error5 ");
		keyList.add("error6 ");
		keyList.add("error7 ");
		keyList.add("error8 ");
		keyList.add("error9 ");
		keyList.add("error10 ");
		keyList.add("error11 ");
		keyList.add("error12 ");
		keyList.add("error13 ");
		keyList.add("error14 ");
		keyList.add("error15 ");
		keyList.add("error16 ");
		keyList.add("error17 ");
		keyList.add("error18 ");
		keyList.add("error19 ");
		keyList.add("help_text ");
		keyList.add("about_text ");

		try {
			Scanner sc = new Scanner(file);
			while (sc.hasNextLine()) {
				String line = sc.nextLine();
				int index = line.indexOf("=");
				if (index > 0) {
					for (int i = 0; i < keyList.size(); i++) {
						String key = line.substring(0, index);
						if (key.equals(keyList.get(i))) {
							propList.add(key);
						}
					}
				}

			}

		} catch (FileNotFoundException e) {
			errorController.setErrorMessage(e, config.getResourceBundle()
					.getString("error19"));
		}
		if (propList.size() == keyList.size()) {
			Collections.sort(propList);
			Collections.sort(keyList);
			for (int i = 0; i < propList.size(); i++) {
				if (propList.get(i).equals(keyList.get(i)) == false) {
					return false;

				}
			}
			return true;
		}
		return false;

	}

	@Override
	public void movePlugin(File plugin) {
		boolean alreadyExists = false;
		File folder = new File("bin/edu/chl/grupp11/aegas/recources/bundles");
		for (File f : folder.listFiles()) {
			if (f.isFile() && f.getPath().equals(plugin.getPath())) {
				alreadyExists = true;
				break;
			}
		}

		if (alreadyExists == false) {
			try {
				FileUtils.copyFileToDirectory(plugin, new File(
						"bin/edu/chl/grupp11/aegas/recources/bundles"), false);
			} catch (IOException ex) {
				errorController.setErrorMessage(ex, config.getResourceBundle()
						.getString("error19"));
			}
		}
	}

	private File openFileBrowser() {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.addChoosableFileFilter(new FileNameExtensionFilter(
				"Language plugins", "properties"));
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int rVal = fileChooser.showOpenDialog(null);
		if (rVal == JFileChooser.APPROVE_OPTION) {
			return fileChooser.getSelectedFile();
		} else {
			return null;
		}
	}
}
